<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加图片，让图片跟随鼠标移动</title>
  <style>
    h2{
      width: 10px;
      height: 10px;
      position: absolute;
      background: red;
    }
  </style>
</head>
<body>
  <script type="text/javascript">
    document.onclick = function (e) {
      //鼠标只要移动,就会触发事件
      // pageX/Y：文档的水平/垂直坐标位置，只在指定的区域
      var x = e.pageX;
      var y = e.pageY;
      var pic = document.createElement('h2')
      // x轴和y轴减100，让图片移动到鼠标中心
      pic.style.left = x - 10 + 'px';
      pic.style.top = y - 10 + 'px';
      document.body.appendChild(pic)
    }
  </script>
</body>

</html>